package framework.ui;

import org.openems.entity.Entity;
import org.openems.entity.Key;

import framework.DomainService;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class EmsEditBoxWithButton extends Button {

	public EmsEditBoxWithButton(final Key key, final DomainService store) {
		final Entity entity = store.getEntity(key);
		this.setText(entity.getName());
		this.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				showDialog(entity, store);
			}			
		});
	}

	protected void showDialog(final Entity entity, final DomainService store) {
		final Stage stage = new Stage();
		
		AnchorPane root = new AnchorPane();
		Scene scene = new Scene(root);
		stage.setScene(scene);
		root.prefWidth(300);
		root.prefHeight(200);
		VBox vbox = new VBox();
		root.getChildren().add(vbox);
		final TextField text = new TextField();
		text.setText(store.getEntity(entity.getKey()).getValueText());
		vbox.getChildren().add(text);
		Button button = new Button("OK");
		vbox.getChildren().add(button);
		button.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				try {
					entity.setValueText(text.getText());
					store.putEntity(entity);
					stage.close();
				}
				catch (Exception e) {
					
				}
			}			
		});
		stage.show();
	}
}
